package cn.yinyu.queue.module.ecg.service.checktype;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.yinyu.queue.module.ecg.controller.admin.checktype.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.checktype.CheckTypeDO;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;

import cn.yinyu.queue.module.ecg.dal.mysql.checktype.CheckTypeMapper;

import javax.annotation.Resource;

import static cn.yinyu.queue.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.yinyu.queue.module.ecg.enums.ErrorCodeConstants.*;

/**
 * 检查类型 Service 实现类
 *
 * @author majianbo
 */
@Service
@Validated
public class CheckTypeServiceImpl implements CheckTypeService {

    @Resource
    private CheckTypeMapper checkTypeMapper;

    @Override
    public Integer createCheckType(CheckTypeSaveReqVO createReqVO) {
        // 插入
        CheckTypeDO checkType = BeanUtils.toBean(createReqVO, CheckTypeDO.class);
        checkTypeMapper.insert(checkType);
        // 返回
        return checkType.getId();
    }

    @Override
    public void updateCheckType(CheckTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateCheckTypeExists(updateReqVO.getId());
        // 更新
        CheckTypeDO updateObj = BeanUtils.toBean(updateReqVO, CheckTypeDO.class);
        checkTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteCheckType(Integer id) {
        // 校验存在
        validateCheckTypeExists(id);
        // 删除
        checkTypeMapper.deleteById(id);
    }

    private void validateCheckTypeExists(Integer id) {
        if (checkTypeMapper.selectById(id) == null) {
            throw exception(CHECK_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public CheckTypeDO getCheckType(Integer id) {
        return checkTypeMapper.selectById(id);
    }

    @Override
    public PageResult<CheckTypeDO> getCheckTypePage(CheckTypePageReqVO pageReqVO) {
        return checkTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CheckTypeDO> getSimpleCheckTypeList() {
        return checkTypeMapper.simpleCheckTypeList();
    }

}